/**
 * System:      oshys
 * Model:       log
 * Licence:     GNU General Public License v3
 * Author:      sunwl
 * Date:        2010/02/19
 */
package org.oshys.util.logging;
/**
 * 消息级别。<br>
 * 默认的分为8个级别：<br>
 * OFF------关闭，不输出任何消息。<br>
 * FATAL----致命的<br>
 * ERROR----错误<br>
 * WARN-----警告<br>
 * DEBUG----调试<br>
 * INFO-----消息<br>
 * TRACE----跟踪<br>
 * ALL------所有<br>
 * 
 * @author sunwl
 */
public class Level implements Comparable<Level>{
    
	private static final int base = 0x02;
	
	private static final int OFF_LEVEL = base << 0;
	
    private static final int FATAL_LEVEL = base << 1;
    
    private static final int ERROR_LEVEL = base << 2;

    private static final int WARN_LEVEL = base << 3;

    private static final int DEBUG_LEVEL = base << 4;

    private static final int INFO_LEVEL = base << 5;
    
    private static final int TRACE_LEVEL = base << 6;
    
    private static final int ALL_LEVEL = base << 7;
    //级别名称，默认为TRACE
    private String levelName = "TRACE";
    //级别，默认为TRACE
    private int level = TRACE_LEVEL;
    
    protected static final Level OFF = new Level("OFF", OFF_LEVEL);

    protected static final Level FATAL = new Level("FATAL", FATAL_LEVEL);
    
    protected static final Level ERROR = new Level("ERROR", ERROR_LEVEL);
    
    protected static final Level WARN = new Level("WARN", WARN_LEVEL);
    
    protected static final Level DEBUG = new Level("DEBUG", DEBUG_LEVEL);
    
    protected static final Level INFO = new Level("INFO", INFO_LEVEL);
    
    protected static final Level TRACE = new Level("TRACE", TRACE_LEVEL);
    
    protected static final Level ALL = new Level("ALL", ALL_LEVEL);
    
    private Level (String name, int level) {
    	this.levelName = name;
    	this.level = level;
    }
    
    /**
     * 通过一个级别名称取得一个Level实例<br>
     * 当查找失败，默认返回INFO
     * @param levelName
     * @return 
     */
    protected static Level getLevel(String levelName){
    	String name = levelName.trim().toUpperCase();
    	if("OFF".equals(name)){
    		return OFF;
    	}else if("FATAL".equals(name)){
    		return FATAL;
    	}else if("ERROR".equals(name)){
    		return ERROR;
    	}else if("WARN".equals(name)){
    		return WARN;
    	}else if("DEBUG".equals(name)){
    		return DEBUG;
    	}else if("INFO".equals(name)){
    		return INFO;
    	}else if("TRACE".equals(name)){
    		return TRACE;
    	}else if("ALL".equals(name)){
    		return ALL;
    	}else{
    		return INFO; 
    	}
    }
    
    protected int getLevel(){
    	return this.level;
    }

    protected String getName(){
    	return this.levelName;
    }
    /**
     * Comparable接口实现。<br>
     * 比较两个Level。<br> 
     * eg:<br>
     * info.compareTo(error) ==> 1<br>
     * debug.compareTo(info) ==> -1
     */
	public int compareTo(Level lvl) {
		if(this.level < lvl.getLevel()){
			return -1;
		}else if(this.level > lvl.getLevel()){
			return 1;
		}else{
			return 0;
		}
	}
}

